package com.enjoy.springboot;

import com.enjoy.springboot.interfaces.AutoConfiguration;
import com.enjoy.springboot.interfaces.PropertySourceLoader;
import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;

public class WhhImportSelect implements DeferredImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {

        // spi机制
        ServiceLoader<AutoConfiguration> serviceLoader = ServiceLoader.load(AutoConfiguration.class);

        ServiceLoader<PropertySourceLoader> sourceLoaders = ServiceLoader.load(PropertySourceLoader.class);


        List<String> list = new ArrayList<>();

        for (AutoConfiguration configuration : serviceLoader) {
            list.add(configuration.getClass().getName());
        }

        for (PropertySourceLoader sourceLoader : sourceLoaders) {
            list.add(sourceLoader.getClass().getName());
        }
        return list.toArray(new String[]{});
    }
}
